package demo;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @Author: WWQ
 * @Date: 2019/4/8 15:29
 * @Version 1.0
 */
public class StreamAPITest7 {

    /**
     * 终止操作
     * <p>
     * 收集：
     * collect将流的形式转换为其他形式.接收一个collector接口的实现类,用于stream中元素汇总的方法
     * <p>
     * collect（collector c）
     * <p>
     * Collector接口中方法的实现了如何对流执行收集操作，但是Collectors实现类提供了很多静态方法可以很方便的创建常见的收集器
     */

    List<Employee> emps = Arrays.asList(
            new Employee("张三", 18, 9999.99, Employee.Status.VOCATION),
            new Employee("李四", 59, 6666.66, Employee.Status.BUS),
            new Employee("王五", 28, 3333.33, Employee.Status.FREE),
            new Employee("赵六", 8, 7777.77, Employee.Status.BUS),
            new Employee("田七", 38, 5555.55, Employee.Status.VOCATION)
    );

    /**
     * toList();
     */
    @Test
    public void test1() {
        List<String> list = emps.stream().map(Employee::getName).collect(Collectors.toList());
        System.out.println(list);
    }

    /**
     * toSet();
     */
    @Test
    public void test2() {
        Set<String> set = emps.stream().map(Employee::getName).collect(Collectors.toSet());
        System.out.println(set);
    }

    /**
     * toCollection();
     */
    @Test
    public void test3() {
        HashSet<String> set = emps.stream().map(Employee::getName).collect(Collectors.toCollection(HashSet::new));
        System.out.println(set);
    }

    /**
     * 总数
     */
    @Test
    public void test4() {
        Long aLong = emps.stream().collect(Collectors.counting());
        System.out.println(aLong);
    }

    /**
     * 平均值
     */
    @Test
    public void test5() {
        Double avg = emps.stream().collect(Collectors.averagingDouble(Employee::getSalary));
        System.out.println(avg);
    }

    /**
     * 最大值
     */
    @Test
    public void test6() {
        Optional<Employee> optionalEmployee = emps.stream().collect(Collectors.maxBy((x, y) -> Double.compare(x.getSalary(), y.getSalary())));
        System.out.println(optionalEmployee);
    }

    /**
     * 最小值
     */
    @Test
    public void test7() {
        Optional<Double> optionalEmployee = emps.stream().map(Employee::getSalary).collect(Collectors.minBy(Double::compare));

        System.out.println(optionalEmployee.get());
    }

    /**
     * 分组
     */
    @Test
    public void test8(){
        Map<Employee.Status,List<Employee>> map=emps.stream().collect(Collectors.groupingBy(Employee::getStatus));
        System.out.println(map);
    }

    /**
     * 多重分组
     */
    @Test
    public void test9(){
        Map<Employee.Status,Map<String,List<Employee>>> map=emps.stream().collect(Collectors.groupingBy(Employee::getStatus,Collectors.groupingBy((e)->{
            if(((Employee)e).getAge()<=35){
                return "青年";
            }else if(((Employee) e).getAge()<=50){
                return "中年";
            }else{
                return "老年";
            }

        })));
        System.out.println(map);
    }

    /**
     * 分区
     */
    @Test
    public void test10(){
        Map<Boolean,List<Employee>>map=emps.stream().collect(Collectors.partitioningBy((e)->e.getSalary()>=8000));
        System.out.println(map);
    }

    /***
     * 计算类
     */
    @Test
    public void test11(){
        DoubleSummaryStatistics ds=emps.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
        System.out.println(ds.getCount());
        System.out.println(ds.getSum());
        System.out.println(ds);
    }

    /**
     * 拼接字符串
     */
    @Test
    public void test12(){
       String str= emps.stream().map(Employee::getName).collect(Collectors.joining(",","[","]"));
        System.out.println(str);
    }
}
